##################################################
# Replication Code
# Taeyong Park and Andrew Reeves
# "Local Unemployment and Voting for President: Uncovering Causal Mechanisms"
# Summary: Replicate Figure 3, the main results.
##################################################

rm(list = ls())
library(foreign)
source("intervalEst.R") # This function produces a summary table of the estimated results 


# 2008 
dataUnemp08=read.table("output_unemp08")
results08=intervalEst(dataUnemp08)

# 2012 
dataUnemp12=read.table("output_unemp12")
results12=intervalEst(dataUnemp12)

# 2016 
dataUnemp16=read.table("output_unemp16")
results16=intervalEst(dataUnemp16)


#################################
# PLOT                          #
#################################

ruler=3:1
nRuler=length(ruler)

par(mfrow=c(1,3), 
    mar=c(2, 0.5, 5, 0.5), 
    oma = c(4, 2, 0, .1), cex=1.1)
plot(x=results08[[1]], y=ruler, 
     xlab="",
     ylab="",
     main="2008",
     xlim=c(-.02, .01), 
     ylim=c(0.75, 3.25),
     pch=16, cex=0.9,
     axes=F)
for(i in 1:nRuler){
  segments(x0=results08[[2]][(i-1)*2+1],
           x1=results08[[2]][(i-1)*2+2],
           y0=ruler[i],
           y1=ruler[i])
}
axis(3, at=c(-.02, -.01, -0, .01),
     labels=c("-2%", "-1", "0", "1"))
axis(1, at=c(-.02, -.01, -0, .01),
     labels=c("-2%", "-1", "0", "1"))
abline(v=0, lty=2)

plot(x=results12[[1]],
     y=ruler,
     xlab="",
     ylab="",
     main="2012",
     xlim=c(-.01, .02), 
     ylim=c(0.75, 3.25),
     pch=16, cex=0.9,
     axes=F)
axis(3, at=c(-.01, -0, .01, .02),
     labels=c("-1%", "0", "1", "2"))
axis(1, at=c(-.01, -0, .01, .02),
     labels=c("-1%", "0", "1", "2"))
abline(v=0, lty=2)
for(i in 1:nRuler){
  segments(x0=results12[[2]][(i-1)*2+1],
           x1=results12[[2]][(i-1)*2+2],
           y0=ruler[i],
           y1=ruler[i])
}

plot(x=results16[[1]],
     y=ruler,
     xlab="",
     ylab="",
     main="2016",
     xlim=c(-.015, .02), 
     ylim=c(0.75, 3.35),
     pch=16, cex=0.9,
     axes=F)
axis(3, at=c(-.01, -0, .01, .02),
     labels=c("-1%", "0", "1", "2"))
axis(1, at=c(-.01, -0, .01, .02),
     labels=c("-1%", "0", "1", "2"))
abline(v=0, lty=2)
for(i in 1:nRuler){
  segments(x0=results16[[2]][(i-1)*2+1],
           x1=results16[[2]][(i-1)*2+2],
           y0=ruler[i],
           y1=ruler[i])
}
mtext("AME", side=2, adj=0.68, outer=T, cex=1.2)
mtext("ADE", side=2, adj=0.42, outer=T, cex=1.2)
mtext("ATE", side=2, adj=0.17, outer=T, cex=1.2)
mtext("The likelihood of voting for the incumbent party", 
      side=1, padj=3, outer=TRUE, cex=1.2)



